package cn.gson.boot.model.service;

import cn.gson.boot.model.dao.*;
import cn.gson.boot.model.mapper.*;
import cn.gson.boot.model.pojos.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class FaHuoService {
    @Autowired
    FaHuoDao faHuoDao;
    @Autowired
    FaHuoXiangQinDao faHuoXiangQinDao;
    @Autowired
    FaHuoMapper faHuoMapper;
    @Autowired
    FaHuoXiangQinMapper faHuoXiangQinMapper;

    @Autowired
    JiaoFuJiLuLuLuDao jiaoFuJiLuLuLuDao;
    @Autowired
    JiaoFuJiLuLuLuXiangQingDao jiaoFuJiLuLuLuXiangQingDao;

    @Autowired
    DinDanDao dinDanDao;
    @Autowired
    ShenHeDao shenHeDao;

    @Autowired
    JiaoFuJiHuaDao jiaoFuJiHuaDao;

    @Autowired
    JiaoFuJiHuaXiangQingMapper jiaoFuJiHuaXiangQingMapper;
    @Autowired
    HuanHuoXiangQinMapper huanHuoXiangQinMapper;

    @Autowired
    DingDanXiangQingMapper dingDanXiangQingMapper;

    //审核同意后，确定发货
    //审核同意后修改订单状态和交付计划的状态,新增交付记录和交付记录详情
    public void quedingFaHuo(Fahuo fahuo){
        String pd = "";
        if(fahuo.getFhDdhh().equals("订单")){
            pd = "订单";
            //fahuo.setHuanhuoByHhId(null);
            Fahuoxiangqing hx = null;
            Jiaofujilu jiaofujilu = new Jiaofujilu();
            jiaofujilu.setJlYs(1);
            jiaofujilu.setDingdanByDdId(fahuo.getDingdanByDdId());
            jiaofujilu.setJlJieshouren(fahuo.getFhShouhuoren());
            jiaofujilu.setJlTime(fahuo.getFhTime());
            jiaofujilu.setkH(fahuo.getkH());
            jiaoFuJiLuLuLuDao.save(jiaofujilu);

            List<Jiaofujihuaxiangqing> jjxq = jiaoFuJiHuaXiangQingMapper.ckJJXQbyjjId(fahuo.getJiaofujihua().getJjId());

            Jiaofujluxiangqing dx = null;
            double money = 0;
            for (int i=0;i<jjxq.size();i++){
                dx = new Jiaofujluxiangqing();
                dx.setProductt(jjxq.get(i).getProductt());
                dx.setJfjlxqCpnum(jjxq.get(i).getJfjhxqNum());
                dx.setJfjlxqCpprice(jjxq.get(i).getJfjhxqPrice());
                dx.setJiaofujiluByJlId(jiaofujilu);
                dx.setJfjlxqXiaoJi(jjxq.get(i).getJfjhXiaoJi());
                jiaoFuJiLuLuLuXiangQingDao.save(dx);
                money += jjxq.get(i).getJfjhXiaoJi().doubleValue();
            }
            jiaofujilu.setJlMoney(new BigDecimal(money));


            dinDanDao.updateDingDanZt("执行中",fahuo.getDingdanByDdId().getDdId());

           //修改交付计划状态，这里===============================
            //修改交付计划的状态(根据交计id)
            jiaoFuJiHuaDao.updateJiaoFuJiHuaZt("已交付",fahuo.getJiaofujihua().getJjId());

            //如果订单总额等于订单回款金额的话， 并且 此订单下的交付计划状态为已交付的所有交付计划
            // 交计详情中的产品类型和产品数量和订单详情表里面的一致,就修改订单状态为已完成
            Dingdan byId = dinDanDao.findById(fahuo.getDingdanByDdId().getDdId()).get();
            boolean hk = false;
            boolean jf = false;
            //判断订单应会金额是否等于已回金额
            if(byId.getDdMoney().equals(byId.getDdHkmoney())){
                hk = true;
            }
            //根据订单id找到订单详情
            List<Dingdanxiangqing> ddxq = dingDanXiangQingMapper.ckDingDanXiangQing(byId.getDdId());
            Integer jjxqnum = 0;
            for(int i=0;i<ddxq.size();i++){
                //根据订单id和产品从表id找到 交付计划状态为已交付的交计详情产品的数量
                jjxqnum = jiaoFuJiHuaXiangQingMapper.selectJJXQandYJFByddId(byId.getDdId(),ddxq.get(i).getProductt().getProducttId());
                // 判断此此产品在订单详情中的数量和在交计详情中的数量是否一致
                if(ddxq.get(i).getDdcpNum() == jjxqnum){
                    jf = true;
                }
            }
            System.err.println("发货hk:"+hk);
            System.err.println("发货jf:"+jf);
            if(jf && hk){
                dinDanDao.updateDingDanZt("已完成",byId.getDdId());
            }

        }else if(fahuo.getFhDdhh().equals("换货单")){
            Jiaofujilu jiaofujilu = new Jiaofujilu();
            jiaofujilu.setJlYs(1);
            //jiaofujilu.setHuanhuoByHhId(fahuo.getHuanhuoByHhId());
            jiaofujilu.setJlJieshouren(fahuo.getFhShouhuoren());
            jiaofujilu.setJlTime(fahuo.getFhTime());
            jiaofujilu.setkH(fahuo.getkH());
            jiaoFuJiLuLuLuDao.save(jiaofujilu);

            List<Dingdanxiangqing> hh = dingDanXiangQingMapper.ckDingDanXiangQing(fahuo.getDingdanByDdId().getDdId());

            Jiaofujluxiangqing dx = null;
            double money = 0;
            for (int i=0;i<hh.size();i++){
                dx = new Jiaofujluxiangqing();
                dx.setProductt(hh.get(i).getProductt());
                dx.setJfjlxqCpnum(hh.get(i).getDdcpNum());
                dx.setJfjlxqCpprice(hh.get(i).getDdcpPrice());
                dx.setJiaofujiluByJlId(jiaofujilu);
                dx.setJfjlxqXiaoJi(hh.get(i).getDdxqXiaoji());

                jiaoFuJiLuLuLuXiangQingDao.save(dx);
                money += hh.get(i).getDdxqXiaoji().doubleValue();
            }

            jiaofujilu.setJlMoney(new BigDecimal(money));
        }


        //修改发货的状态
        faHuoDao.updateFaHuoZt("发货成功",fahuo.getFhId());

    }

    //新增发货
    public void addFaHuo(Fahuo fahuo) {
        fahuo.setFhYs(1);
        System.err.println(fahuo.getFhDdhh());
        String pd = "";

        if(fahuo.getFhDdhh().equals("订单")){
            pd = "订单";
            Fahuoxiangqing hx = null;

            for (int i=0;i<fahuo.fhJiaoJiXiangQing.size();i++){
                hx = new Fahuoxiangqing();
                hx.setFhcpNum(fahuo.fhJiaoJiXiangQing.get(i).getJfjhxqNum());
                hx.setFhcpPrice(fahuo.fhJiaoJiXiangQing.get(i).getJfjhxqPrice());
                hx.setProductt(fahuo.fhJiaoJiXiangQing.get(i).getProductt());
                hx.setFahuoByFhId(fahuo);
                hx.setFhcpXiaoji(fahuo.fhJiaoJiXiangQing.get(i).getJfjhXiaoJi());
                faHuoXiangQinDao.save(hx);
            }

        }else if(fahuo.getFhDdhh().equals("换货单")){
            fahuo.setJiaofujihua(null);
            Fahuoxiangqing hx = null;
            for (int i=0;i<fahuo.fhDingDanXiangQing.size();i++){
                hx = new Fahuoxiangqing();
                hx.setFhcpNum(fahuo.fhDingDanXiangQing.get(i).getDdcpNum());
                hx.setFhcpPrice(fahuo.fhDingDanXiangQing.get(i).getDdcpPrice());
                hx.setProductt(fahuo.fhDingDanXiangQing.get(i).getProductt());
                hx.setFahuoByFhId(fahuo);
                hx.setFhcpXiaoji(fahuo.fhDingDanXiangQing.get(i).getDdxqXiaoji());
                faHuoXiangQinDao.save(hx);
            }

        }

        Fahuo fhOk = faHuoDao.save(fahuo);
        quedingFaHuo(fhOk);
    }

    //查询所有发货（根据订单号模糊查询）
    public List<Fahuo> selectAllFaHuo(String ddHao,String khName,String time,String fhZt,String empName){
        return faHuoMapper.selectAllFaHuo(ddHao,khName,time,fhZt,empName);
    }

    //根据发货id查看发货详情
    public List<Fahuoxiangqing> selectFaHuoXiangQin(Integer fhId) {
        return faHuoXiangQinMapper.selectFaHuoXiangQin(fhId);
    }

    //移除发货
    public void yichuFaHuo(Integer fhId) {
        faHuoDao.yichuFaHuo(fhId);
    }

    //根据发货id查看发货
    public Fahuo selectFaHuoByfhId(Integer fhId) {
        return faHuoMapper.selectFaHuoByfhId(fhId);
    }




    //{退货}据ddId查到此订单的所有发货状态为‘发货成功’的发货单
    public List<Fahuo> selectFaHuoByddIdTHfhcg(Integer ddId){
        return faHuoMapper.selectFaHuoByddIdTHfhcg(ddId);
    }
}